package ncrnadb.ncinetview.internal;

import java.util.Set;
import java.util.TreeSet;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNetworkFactory;
import org.cytoscape.model.CyNetworkManager;
import org.osgi.framework.BundleContext;

public class NcINetViewApp {

	
	private static NcINetViewApp app = new NcINetViewApp();
	
	private NcINetViewApp(){
	}
	
	public static NcINetViewApp app(){
		return app;
	}
	
	
	
	private BundleContext bc = null;
	public void setBundleContext(BundleContext bc){
		this.bc = bc;
	}
	public BundleContext bc(){
		return bc;
	} 
	
	private CyActivator ca = null;
	public void setCyActivator(CyActivator ca){
		this.ca = ca;
	}
	public CyActivator ca(){
		return this.ca;
	}
	
        
	public CyNetworkManager cnm = null;
    public CyNetworkFactory networkFactory = null;
    public CyNetworkManager networkManager = null;
        
        
    public static String baseNewNetName = "ncINetView";
    
    public CyNetwork createNewNetwork(){
    	return createNewNetwork(baseNewNetName);
    }
    public CyNetwork createNewNetwork(String base){
        //CyNetworkFactory networkFactory = ca.getService(bc, CyNetworkFactory.class);
        Set<String> names = new TreeSet<String>(); 
        for(CyNetwork net : cnm.getNetworkSet()){
            names.add(net.getRow(net).get(CyNetwork.NAME, String.class));
        }
        
        int i=0;
        while(true){
            if(!names.contains(base+"-"+i))
                break;
            i++;
        }
        
        
        CyNetwork net = networkFactory.createNetwork();
        net.getRow(net).set(CyNetwork.NAME, base+"-"+i);
        networkManager.addNetwork(net);
        return net;
    }
        
        
    
        
    
}
